package com.avcdata.aoscustommade.model;

import android.content.Context;

import com.avcdata.aoscustommade.activity.ChangePhoneOrEmail_Activity;
import com.avcdata.aoscustommade.main.BaseCallBack;
import com.avcdata.aoscustommade.utils.AvcConstants;
import com.avcdata.aoscustommade.utils.http.AvcHttpUtils;
import com.avcdata.aoscustommade.utils.http.AvcJsonHttpResponseHandler;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 修改账号的model
 * Created by zyh on 17/6/12.
 */

public class ChangePhoneOrEmailModel {
    private final Context mContext;

    public ChangePhoneOrEmailModel(Context context) {
        this.mContext = context;
    }

    /**
     * 获取验证码
     * @param type
     * @param number
     * @param baseCallBack
     */
    public void lodeCode(boolean type, String number, final BaseCallBack baseCallBack) {
        String url = AvcConstants.URLHeaderUserinfo;
        JSONObject jsonObject = new JSONObject();
        try {
            if (type) {
                jsonObject.put("telephone", number);
                url = url + "sendVerifyNum";
            } else {
                jsonObject.put("email", number);
                url = url + "sendVerifyByMail";
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        AvcHttpUtils.post(url, jsonObject, new AvcJsonHttpResponseHandler(mContext) {

            @Override
            public void onSuccess(JSONObject jsonObject) {
                baseCallBack.onSuccess(jsonObject);
            }

            @Override
            public void onFailure() {
                super.onFailure();
                baseCallBack.onFailure();
            }


        });
    }

    /**
     * 验证验证码
     * @param telephone
     * @param code
     * @param baseCallBack
     */
    public void loadCheckCode(String telephone, String code, final BaseCallBack baseCallBack) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("telephone", telephone);
            jsonObject.put("code", code);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        AvcHttpUtils.post(AvcConstants.URLHeaderUserinfo + "ifVerifyPass", jsonObject, new AvcJsonHttpResponseHandler(mContext) {

            @Override
            public void onSuccess(JSONObject jsonObject) {
                super.onSuccess(jsonObject);
                baseCallBack.onSuccess(jsonObject);
            }

            @Override
            public void onFailure() {
                super.onFailure();
                baseCallBack.onFailure();
            }

        });
    }

    /**
     * 账号重置
     * @param type
     * @param oldName
     * @param newName
     * @param baseCallBack
     */
    public void loadReset(boolean type, String oldName, String newName, final BaseCallBack baseCallBack) {
        JSONObject jsonObject = new JSONObject();
        try {
            if (type) {
                jsonObject.put("oldUsername", oldName);
                jsonObject.put("newUsername", newName);
            } else {
                jsonObject.put("oldEmail", oldName);
                jsonObject.put("newEmail", newName);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        AvcHttpUtils.post(AvcConstants.URLHeaderUserinfo + "updateUsernameOrPassword", jsonObject, new AvcJsonHttpResponseHandler(mContext) {

            @Override
            public void onSuccess(JSONObject jsonObject) {
                super.onSuccess(jsonObject);
                baseCallBack.onSuccess(jsonObject);

            }


            @Override
            public void onFailure() {
                super.onFailure();
                baseCallBack.onFailure();
            }

        });
    }
}
